// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#ifndef __H_MK_PAINTER__
#define __H_MK_PAINTER__

#include "mk_render_input.h"
#include "mk_hardware_buffer.h"
#include "mk_texture.h"
#include "mk_rect.h"
#include "mk_shader.h"
#include "mk_aabb.h"

namespace MK3DE
{
	class MK3DE_API CPainter
	{
	public:
		CPainter();
		~CPainter();

		BOOL Init(DWORD bufferSize);

		//! Draw a 2d line.
		BOOL DrawLine2D(D3DXVECTOR2& pos1, D3DXVECTOR2& pos2, DWORD color1, DWORD color2);

		//! Draw a 2d rectangle(empty)
		BOOL DrawRect(Rect2D& rect, RectColor& color);        

		//! Draw a texture with specified 2d destination area and vertex color.
		BOOL DrawImage2D(Rect2D& rect, RectColor& color, CTexture2D* pTexture);

		//! Draw the sub-area of a texture with 2d specified destination area and vertex color.
		BOOL DrawImageSub2D(Rect2D& rect, Rect2D& srcRect, RectColor& color, CTexture2D* pTexture);

		//! Draw a 3d line.
		BOOL DrawLine3D(D3DXVECTOR3& pos1, D3DXVECTOR3& pos2, DWORD color1, DWORD color2);

		//! Draw a 3d triangle.
		BOOL DrawTriangle(D3DXVECTOR3& pos1, D3DXVECTOR3& pos2, D3DXVECTOR3& pos3, D3DXVECTOR2& uv1, D3DXVECTOR2& uv2, D3DXVECTOR2& uv3, DWORD color1, DWORD color2, DWORD color3, CTexture2D* pTexture);

		//! Draw a 3d Axis-Align-Bounding-Box.
		BOOL DrawAABB(AABB& aabb, DWORD color);

		//! Flush all buffered render operations.
		BOOL Flush();

	private:
		BOOL __FlushLine2D();
		BOOL __FlushLine3D();
		BOOL __FlushQuad2D();
		BOOL __FlushTriangle3D();

	private:
		struct LINE_2D
		{
			VERT_2D_COL_TEX1 v[2];
		};

		struct QUAD_2D
		{
			VERT_2D_COL_TEX1 v[6];
		};

		struct LINE_3D
		{
			VERT_3D_COL_TEX1 v[2];
		};

		struct TRIANGLE_3D
		{
			VERT_3D_COL_TEX1 v[3];
		};

		LINE_2D* _bufferLine2D;
		CHardwareBuffer* _vbLine2D;
		CRenderInput* _riLine2D;

		QUAD_2D* _bufferQuad2D;
		CHardwareBuffer* _vbQuad2D;
		CRenderInput* _riQuad2D;

		LINE_3D* _bufferLine3D;
		CHardwareBuffer* _vbLine3D;
		CRenderInput* _riLine3D;

		TRIANGLE_3D* _bufferTriangle3D;
		CHardwareBuffer* _vbTriangle3D;
		CRenderInput* _riTriangle3D;

		DWORD _size;
		DWORD _count;    

		CTexture2D* _currentTexture;
		int _currentPrimitiveType;

		CShader* _shader;
	};

};

#endif // __H_MK_PAINTER__